//
//  CodeNumberView.m
//  Project_IOS
//
//  Created by Jeff on 2022/1/18.
//

#import "CodeNumberView.h"
#import <Masonry/Masonry.h>
#import "Color.h"
@interface CodeNumberView ()


@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIView *textView;

@property (nonatomic, strong) UILabel *codeLb;

@property (nonatomic, assign) NSInteger countNum;

@end

@implementation CodeNumberView
- (instancetype)init {
    self = [super init];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(311, 52));
        make.top.left.mas_equalTo(0);
    }];
    
    [self.contentView addSubview:self.textField];
    [self.contentView addSubview:self.moreBtn];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.moreBtn.mas_left).offset(-20);
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
    }];
        
    
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(76, 22));
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
    }];
    self.countNum = 0;
}

- (void)startCount:(NSInteger)count {
    self.countNum = count;
    
    if (count == 0) {
        return;
    }
//    WEAKSELF;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
//        if(weakSelf.countNum<=0){//倒计时结束，关闭
//            dispatch_source_cancel(_timer);
//        }else{
//            weakSelf.countNum--;
//        }
    });
    dispatch_resume(_timer);
    
}

- (void)setCountNum:(NSInteger)countNum {
    _countNum = countNum;
//    WEAKSELF;
    dispatch_async(dispatch_get_main_queue(), ^{
//        if (weakSelf.countNum == 0) {
//            [weakSelf.moreBtn setTitle:@"重新发送" forState:UIControlStateNormal];
//            weakSelf.moreBtn.userInteractionEnabled = true;
//            [weakSelf.moreBtn setTitleColor:[Color colorWithHexString:@"#2075FD"] forState:UIControlStateNormal];
//        }else{
//            weakSelf.moreBtn.userInteractionEnabled = false;
//            [weakSelf.moreBtn setTitle:[NSString stringWithFormat:@"%lds",countNum] forState:UIControlStateNormal];
//            [weakSelf.moreBtn setTitleColor:[Color colorWithHexString:@"#999999"] forState:UIControlStateNormal];
//        }
    });
}


- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [Color colorWithHexString:@"#F7F7F7"];
        _contentView.layer.cornerRadius = 26;
        _contentView.layer.masksToBounds = true;
        
    }
    return _contentView;
}


- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.textColor = [Color colorWithHexString:@"#333333"];
        _textField.font = [UIFont systemFontOfSize:16];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.placeholder = @"请输入验证码";
        _textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _textField;
}

- (UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn setTitle:@"重新发送" forState:UIControlStateNormal];
        _moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_moreBtn addTarget:self action:@selector(showSecure) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

- (void)showSecure {
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
